home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / MESSAGE.C < prev    next >
C/C++ Source or Header  |  1997-05-24  |  3KB  |  125 lines

  1. #include "global.h"
  2. #include "commands.h"
  3. #include "netuser.h"
  4. #ifndef MSDOS
  5. #include <time.h>
  6. #include "ctype.h"
  7. #include "session.h"
  8. #endif
  9. #ifdef MESSAGESERVER
  10.  
  11.  
  12. #if !defined(_lint)
  13. static char rcsid[] OPTIONAL = "$Id: message.c,v 1.2 1997/05/24 22:13:02 root Exp root $";
  14. #endif
  15.  
  16. #define MAXMESSAGEPORTS 10
  17. struct m_portsused {
  18.     int    port;
  19.     int    sock;
  20.     uint32    address;
  21.     char    *message;
  22. };
  23. static struct m_portsused ports[MAXMESSAGEPORTS];
  24.  
  25. static void messageserv (int s, void *v1, void *p OPTIONAL);
  26.  
  27.  
  28.  
  29. /* Start up the message service */
  30. int
  31. message1 (int argc, char *argv[], void *p OPTIONAL)
  32. {
  33. int port, k;
  34. uint32 address = INADDR_ANY;
  35.  
  36.     port = atoi(argv[1]);
  37.  
  38.     if (argc > 3)    {
  39.         address = resolve (argv[3]);
  40.         if (address == 0L)    {
  41.             tprintf ("IP address %s does NOT resolve! MESSAGE server NOT started on port %d!\n",
  42.                 argv[3], port);
  43.             return 0;
  44.         }
  45.     }
  46.  
  47.     for (k = 0; k < MAXMESSAGEPORTS; k++) {
  48.         if (ports[k].port == port && ports[k].address == address)    {
  49.             tprintf ("Sorry, but you already have a MESSAGE server on port #%d for that address!\n", port);
  50.             return 0;
  51.         }
  52.     }
  53.  
  54.     for (k = 0; k < MAXMESSAGEPORTS; k++) {
  55.         if (ports[k].sock <= 0)
  56.             break;
  57.     }
  58.     if (k == MAXMESSAGEPORTS)    {
  59.         tprintf ("Sorry, but all %d MESSAGE ports are assigned!\n", MAXMESSAGEPORTS);
  60.         return 0;
  61.     }
  62.  
  63.     ports[k].message = strdup (argv[2]);
  64.     ports[k].sock = -1;
  65.     ports[k].port = port;
  66.     ports[k].address = address;
  67.  
  68.     return (installserver (argc, argv, &ports[k].sock, "MESSAGE Listener", ports[k].port,
  69.         ports[k].address, "MESSAGE Server", messageserv, 1024, (void *)k));
  70. }
  71.  
  72.  
  73.  
  74. int
  75. message0 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  76. {
  77. int port, k;
  78. uint32 address = INADDR_ANY;
  79.  
  80.     port = atoi(argv[1]);
  81.     if (argc > 2)    {
  82.         address = resolve (argv[2]);
  83.         if (address == 0L)    {
  84.             tprintf ("IP address %s does NOT resolve! MESSAGE server NOT stopped on port %d!\n",
  85.                 argv[2], port);
  86.             return 0;
  87.         }
  88.     }
  89.     for (k = 0; k < MAXMESSAGEPORTS; k++) {
  90.         if (ports[k].port == port && ports[k].address == address)    {
  91.             ports[k].port = 0;
  92.             free (ports[k].message);
  93.             ports[k].message = NULLCHAR;
  94.             return (deleteserver (&ports[k].sock));
  95.         }
  96.     }
  97.     tprintf ("Sorry, but no MESSAGE server was found on port #%d for that address!\n", port);
  98.     return 0;
  99. }
  100.  
  101.  
  102.  
  103. static void
  104. messageserv (int s, void *v1, void *p OPTIONAL)
  105. {
  106. int theindex;
  107.     
  108.     theindex = (int) v1;
  109.  
  110.     (void) sockowner (s, Curproc);
  111.     (void) sockmode (s, SOCK_ASCII);
  112.     close_s(Curproc->output);
  113.     close_s(Curproc->input);
  114.     Curproc->output = Curproc->input = s;
  115.  
  116.     usprintf (s, "%s\n", ports[theindex].message);
  117.         usflush (s);
  118.     kpause (1000);
  119.     close_s (s);
  120. }
  121.  
  122.  
  123. #endif
  124.  
  125.